package demo.BlockingQueueDemo;

import java.text.MessageFormat;
import java.util.Random;
import java.util.concurrent.BlockingQueue;

/**
 * Created by zhangyanfei on 2018/4/12.
 *
 * @author zhangyanfei
 * @date 2018/04/12
 */
public class Consumer implements Runnable {
    private BlockingQueue<PcData> queue;
    private static final int SLEEPTIME = 1000;

    public Consumer(BlockingQueue<PcData> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        System.out.println("start Consumer id:" + Thread.currentThread().getId());
        Random r = new Random();
        try {
            while (true) {
                PcData pcData = queue.take();
                if (pcData != null) {
                    int re = pcData.getData() * pcData.getData();
                    System.out.println(MessageFormat.format("{0}*{1}={2}", pcData.getData(), pcData.getData(), re));
                    Thread.sleep(r.nextInt(SLEEPTIME));
                }
            }
        } catch (Exception e) {

        }
    }
}
